package org.swidgets.base.smessagebox;

import org.swidgets.base.slabel.SLabel;

import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Widget;





 public class SMessageBox {	

	static private String strButtonOkText = "Ok";
	static private String strButtonCancelText = "Cancel";
	static private String strButtonSiText = "Yes";
	static private String strButtonNoText = "No";
	
	static private SDialogBox sDialogBox;
	static private HorizontalPanel buttonPanel;
	static private Button ok;
	static private Button cancel;
	static private Button yes;
	static private Button no;
	
	static ConfirmCallBack externCallBack;
	
	public static final void alert(String message, String title, ConfirmCallBack callBack){
		final SDialogBox dialog = getDialog(true, false, false, false,callBack);
		final ConfirmCallBack CALLBACK = callBack;
		dialog.setCaption(title);
		SLabel msg = new SLabel(message);	
		dialog.setBodyWidget(msg);	
		dialog.show();
		dialog.center();
		
//		ok.addClickListener(new ClickListener() {
//			public void onClick(Widget sender) {				
//				dialog.hide();
//				if(CALLBACK!=null)
//					CALLBACK.onConfirm();
//			}			
//		});		
	}
	
	public static final void alert(String message, ConfirmCallBack callBack){
		alert(message, "", callBack);		
	}
	public static final void alert(String message){
		alert(message, "", null);		
	}
	

	
	
	
	public static final void confirm (String message, String title, ConfirmCallBack callBack){
		final ConfirmCallBack CALLBACK = callBack;
		final SDialogBox dialog = getDialog(false, false, true, true,callBack);
		dialog.setCaption(title);
		SLabel msg = new SLabel(message);
		msg.addStyleName("DialogTraspText");		
		dialog.setBodyWidget(msg);
		
//		yes.addClickListener(new ClickListener() {
//			public void onClick(Widget sender) {				
//				dialog.hide();
//				CALLBACK.onYes();
//			}			
//		});
//		
//		no.addClickListener(new ClickListener() {
//			public void onClick(Widget sender) {				
//				dialog.hide();
//				CALLBACK.onNo();
//			}			
//		});
//				
		dialog.setCaption(title);	
		dialog.show();	
		dialog.center();	
	}
	
	public static final void confirm (String message, ConfirmCallBack callBack){
		confirm(message, "", callBack);
	}
	
	public static final void yesNowCancel(String message, String title, ConfirmCallBack callBack){
		//final ConfirmCallBack CALLBACK = callBack;
		final SDialogBox dialog = getDialog(false, true, true, true,callBack);

		dialog.setCaption(title);
		SLabel msg = new SLabel(message);
		msg.addStyleName("DialogTraspText");		
		dialog.setBodyWidget(msg);
//		yes.addClickListener(new ClickListener() {
//			public void onClick(Widget sender) {				
//				dialog.hide();
//				CALLBACK.onYes();
//			}			
//		});
//
//		no.addClickListener(new ClickListener() {
//			public void onClick(Widget sender) {				
//				dialog.hide();
//				CALLBACK.onNo();
//			}			
//		});
//		
//		cancel.addClickListener(new ClickListener() {
//			public void onClick(Widget sender) {				
//				dialog.hide();
//				CALLBACK.onCancel();
//			}			
//		});				
		dialog.setCaption(title);	
		dialog.show();
		dialog.center();		
		
	}
		
	
	private static SDialogBox getDialog(boolean okVisible, boolean cancelVisible, boolean yesVisible, boolean noVisible, ConfirmCallBack callBack){
		//final ConfirmCallBack CALLBACK = callBack;
		externCallBack=callBack;
		if (sDialogBox == null){
			sDialogBox = new SDialogBox();

			buttonPanel = new HorizontalPanel();
			buttonPanel.setHeight("40px");
			buttonPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
			buttonPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
			ok = new Button(strButtonOkText);
			yes = new Button(strButtonSiText);
			no = new Button(strButtonNoText);
			cancel = new Button(strButtonCancelText);
			
			yes.addClickListener(new ClickListener() {
				public void onClick(Widget sender) {				
					sDialogBox.hide();
					//CALLBACK.onYes();
					if(externCallBack!=null)
						externCallBack.onYes();
				}			
			});

			no.addClickListener(new ClickListener() {
				public void onClick(Widget sender) {				
					sDialogBox.hide();
					//CALLBACK.onNo();
					if(externCallBack!=null)
						externCallBack.onNo();
						
				}			
			});
			
			cancel.addClickListener(new ClickListener() {
				public void onClick(Widget sender) {				
					sDialogBox.hide();
					//CALLBACK.onCancel();
					if(externCallBack!=null)
						externCallBack.onCancel();
				}			
			});		
			
			ok.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {				
				sDialogBox.hide();
//				if(CALLBACK!=null)
//					CALLBACK.onConfirm();
				if(externCallBack!=null)
					externCallBack.onConfirm();
			}			
		});	

			sDialogBox.showFooter();
			sDialogBox.setFooterWidget(buttonPanel);			
			buttonPanel.setSpacing(10);
		}
		
		buttonPanel.clear();		
		if (okVisible) buttonPanel.add(ok);
		if (yesVisible) buttonPanel.add(yes);
		if (noVisible) buttonPanel.add(no);
		if (cancelVisible) buttonPanel.add(cancel);

		return sDialogBox;
	}
}
